/**
 *  SansEmotion
 *  Author: tom
 *  Description: 
 */

model SansEmotion

/* Insert your model definition here */
global{	
	file shape_file_obstacles <- file('../includes/gis/model2/obstacles.shp');
	file shape_file_bounds <- file('../includes/gis/model2/boundary.shp');	
	file shape_file_ports <- file('../includes/gis/model2/ports.shp');
	
	
	////speed stuff/////////////////////////////////
	float basic_speed <- 0.3 min: 0.1 max: 7.0;
	float normal_speed <- basic_speed*1.5;
	float little_fear_speed <- basic_speed * 2;
	float average_fear_speed <- basic_speed * 4;
	float panic_speed <- basic_speed * 8;
	////end speed stuf/////////////////////////////

	////fear thresholds stuff /////////////////////
	float little_fear_threshold <- 0.2;
	float average_fear_threshold <- 0.5;
	float panic_threshold <- 0.8;
	////end fear thresholds stuff /////////////////
		
	list obstacles of: obstacle;
	background bg;
	string img_fire <- '../images/fire.png';	
	float fire_range_of_affect <- 15.0;
	float propagation_probability <- 0.001; 
	
	init {
		create obstacle from : shape_file_obstacles returns: list_of_obstacle ;
		create background from : shape_file_bounds returns: list_of_bg ;
		create port from : shape_file_ports ;
		set obstacles <- obstacles union list ( list_of_obstacle ) ;
		set bg <- first ( list_of_bg) ;
		ask target:bg {
			set shape <- self - obstacles;
		}		
	}
}

entities {
	species obstacle  {
		rgb color <- rgb([200, 200, 200]);
		aspect base {
			draw geometry: shape color: color ;
		}
	}
	
	species port skills: [situated]{
		rgb color <- rgb('green')  ;
		aspect base{
			draw geometry: shape color: color ;		
		}		
	}
	
	species background skills:[situated]{
		rgb color <- rgb('pink')  ;
		aspect base{
			draw geometry: shape color: color ;		
		}
	}
	
	species fire skills:[situated]{
		float range_of_affect <- fire_range_of_affect;
		
        reflex propager{                        
            //let good_candidates type: list of: obstacle <- candidates where
            
            if (flip(propagation_probability)){
            	//let candidates type: list of: point <- ((self neighbours_at range_of_affect) where !(each overlaps self));
                
//                let successful <- false;
//                loop while: not successful{
//                	let candidate <- any_location_in (circle(range_of_affect) - circle(5));
//                	let fires <- candidate neighbours_at 5 of_species fire;
//                	if(fires = nil){
//		                create fire{
//		                	set location <- candidate;
//		                }
//						set successful <- true;                		
//                	}	
//                }
				let candidates type: list of: obstacle <- ((self neighbours_at range_of_affect) of_species obstacle );
				let good_candidates <- candidates - fire overlapping(candidates);
				let candidate <- one_of (good_candidates);
				ask target: candidate{
					create fire{
						set location <- any_location_in(myself);
					}
				}				
            }
        }
		aspect image {
			draw image: img_fire size: 5;  
		}
		
		aspect base { 			
			draw shape : triangle size : 5 color : rgb('red');
		}
	}
}
environment bounds: shape_file_bounds ;